//
//  THJPickerView.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/29.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJPickerView.h"
@interface THJPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)UIPickerView * pickerView;
@end

@implementation THJPickerView

-(UIPickerView*)pickerView{
    if (!_pickerView) {
        //        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        //        [self.view addSubview:view];
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.backgroundColor = SingleColor(whiteColor);
        _pickerView.dataSource = self;
        
    }
    return _pickerView;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}
-(void)setUI{
    self.backgroundColor = COLOR(0, 0, 0, 0.3);
    self.pickerView.frame = CGRectMake(0, ScreenHeight/4*3, ScreenWidth, ScreenHeight/4);
    [self addSubview:self.pickerView];
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight/4*3-40, ScreenWidth, 40)];
    topView.backgroundColor = THJOrgColor;
    [self addSubview:topView];
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitleColor:SingleColor(whiteColor) forState:UIControlStateNormal];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.frame = CGRectMake(0, 0, 50, 40);
    [cancelBtn addTarget:self action:@selector(cancelPicker) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:cancelBtn];
    
    UIButton * sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitleColor:SingleColor(whiteColor) forState:UIControlStateNormal];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.frame = CGRectMake(ScreenWidth-50, 0, 50, 40);
    [sureBtn addTarget:self action:@selector(surePicker) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:sureBtn];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.dataArr.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //NSArray * titleArr = @[@"标准块10-70mm",@"标准块10-50mm",@"自然块",@"粒(0.5-1mm)",@"粒(1-3mm)",@"粒(3-8mm)",@"粉",@"面"];
    return self.dataArr[row];
}
-(void)cancelPicker{
    NSLog(@"取消");
    [self removeFromSuperview];
}
-(void)surePicker{
    //NSArray * titleArr = @[@"标准块10-70mm",@"标准块10-50mm",@"自然块",@"粒(0.5-1mm)",@"粒(1-3mm)",@"粒(3-8mm)",@"粉",@"面"];
    NSInteger index = [self.pickerView selectedRowInComponent:0];
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(selectedItem:Index:)]){
        [self.delegate selectedItem:self.dataArr[index] Index:index];
    }
}
@end
